package com.hurt.competition.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hurt.competition.admin.dto.ApiResult;
import com.hurt.competition.admin.util.ApiResultHandler;
import com.hurt.competition.admin.dto.TeacherReq;
import com.hurt.competition.admin.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/tea")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @GetMapping("/select")
    public ApiResult findAll(@PathVariable Integer page){
        Page<TeacherReq> teacherPage = new Page<>(page, 10);
        IPage<TeacherReq> res = teacherService.findAll(teacherPage);
        return ApiResultHandler.buildApiResult(200, "分页查询所有评委", res);
    }

    @GetMapping("/{teacherId}")
    public ApiResult findById(@PathVariable("p_id") Integer teacherId) {
        TeacherReq res = teacherService.findById(teacherId);
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        } else {
            return ApiResultHandler.buildApiResult(404,"查询的用户不存在",null);
        }
    }

    @DeleteMapping("/{j_id}")
    public ApiResult deleteById(@PathVariable("p_id") Integer teacherId) {
        return ApiResultHandler.buildApiResult(200,"删除成功",teacherService.deleteById(teacherId));
    }

    @PutMapping("/teacher")
    public ApiResult update(@RequestBody TeacherReq teacher) {
        int res = teacherService.update(teacher);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"更新成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"更新失败",res);
    }

}